import request from '@/utils/request'
import { S } from 'mockjs'

type Common<T> = {
    code:string
    data: T
    mesg: string
    time: string
}
export type Role = {
    code: string
    createdBy?: string
    createdTime?: string
    description: string
    id?: number
    name: string
    operatorId?: number  | null
    updatedBy?: string
    updatedTime?: string
}
export type  QueryResult = Partial<{
    current : number
    hitCount : boolean
    optimizeCountSql : boolean
    orders : any[]
    pages : number
    records : Role[] | []
    searchCount : boolean
    size:number
    total : number
}>

export type Condition = Partial<{
    code: string,
    id: number,
    name: string
    startCreateTime: string,
    endCreateTime: string,
    current: number,
    size: number,
    
}>
//按条件查询角色信息
export const getRolePages = (condition: Condition) => {
    return request<Common<QueryResult>>({
        method: 'Post',
        url: '/boss/role/getRolePages',
        data: condition,
    })
}
type RoleParams = Pick<Role, 'code'| 'description' | 'name' |'id'>
//保存或更新角色
export const saveOrUpdate = (role:RoleParams) => {
    return request<Common<Boolean>>({
        method: 'Post',
        url: '/boss/role/saveOrUpdate',
        data: role,
    })
}

//删除角色 
export const deleteRole = (id:number) => {
    return request<Common<Boolean>>({
        method: 'delete',
        url: `/boss/role/${id}`,

    })
    }

//获取指定角色信息
export const getRoleById = (id:number) => {
    return request<Common<Role>>({
        method:'get',
        url: `/boss/role/${id}`,
    })
}

type RoleMenuItem = {
    createBy: string
    createdTime: string
    description: string
    href:string
    icon:string
    id: number
    level:number
    name:string
    operatorId: number|null
    orderNum: number
    parentId: number
    selected: boolean
    shown:boolean
    subMenuList: RoleMenuItem[] | null
    updatedBy: string
    updatedTime: string
}
//获取角色拥有的菜单信息
export const  getRoleMenus = (id:string) => {
    return request<Common<RoleMenuItem[]>>({
        
        method: 'get',
        url: "/boss/menu/getRoleMenus",
        params:{
            roleId:id
        },
    })
}

// 给角色分配菜单信息
export const allocateRoleMenus = (roleId: string,menuIdList:number[]) => {
    return request<Common<Boolean>>({
        method: 'post',
        url: '/boss/menu/allocateRoleMenus',
        data: {
            roleId,
            menuIdList
        }
    })
}
export type RoleResourceItem = {
  categoryId: number
  createdBy: string
  createdTime: string
  description: string
  id: number
  name: string
  operatorId: number|null
  selected: boolean
  updatedBy: string
  updatedTime: string
  url: string
}

export type RoleCategoryItem = {
    createdBy: string
    createdTime: string
    id: number
    name: string
    operatorId: number|null
    resourceList: null|RoleResourceItem[]
    selected: boolean
    sort:number
    updatedBy: string
    updatedTime: string
}
//获取角色拥有的资源列表
export const getRoleResources = (id:string) => {
    return request<Common<RoleCategoryItem[]>>({
     method: 'get',
     url: '/boss/resource/getRoleResources',
     params: {
         roleId: id,
     },
    })
}   
//给角色分配资源
export const allocateRoleResources = (roleId: string, resourceIdList: number[]) => {
    return request<Common<Boolean>>({
        method: 'post',
        url: '/boss/resource/allocateRoleResources',
        data: {
            roleId,
            resourceIdList,
        }
    
}
    )
}